package com.cn.wkditui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import com.cn.wkditui.bean.MainBean;



public class MainActivity extends AppCompatActivity {

    private TextView tvtotal,tvtoday,tv;
    private Button btstart;
    private String staffId;
    private TextView tvphone;
    private TextView tvchange;
    private TextView tv_pro;
    private ListView lv;

    private List<MainBean> beans;
    private ScrollView sc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        load();
        btstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Auth1Activity.class);
                intent.putExtra("staffId", staffId);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        tv_pro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, MapListActivity.class);
                intent .putExtra("staffId",staffId);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        tvchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,WelComeActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                MainActivity.this.finish();
            }
        });

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

//        if(keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键
//            return false;
//        }
        return super.onKeyDown(keyCode, event);
    }

    private void init() {
        Log.e("init","scorllto");
        staffId =getIntent().getStringExtra("staffId");
        tvtoday = (TextView) findViewById(R.id.tvtoday);
        tv = (TextView) findViewById(R.id.tv);
        tvtotal = (TextView) findViewById(R.id.tvtotal);
        btstart = (Button) findViewById(R.id.btstart);
        tvphone = (TextView) findViewById(R.id.tvphone);
        tvchange = (TextView) findViewById(R.id.tvchange);
        tvphone.setText(staffId);
        tv.requestFocus();
        tv.setFocusable(true);
        lv = (ListView) findViewById(R.id.lv);
        tv_pro = (TextView)findViewById(R.id.tv_producter);
        beans = new ArrayList<>();
        sc = (ScrollView) findViewById(R.id.sc);
        sc.smoothScrollTo(0, 20);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        sc.smoothScrollTo(0, 20);
        load();
    }

    private void load() {
        RequestParams params = new RequestParams();
        params .addBodyParameter("phone",MyPreference.getInstance(this).getStaffid());
        new HttpUtils().send(HttpRequest.HttpMethod.POST, Consts.HOST + Consts.GETRESULT, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e("main","mainde结构"+responseInfo.result);
                beans.clear();
                try {
                    JSONObject json = new JSONObject(responseInfo.result);
                    if(json.getInt("code")==1){
                        tvtotal.setText(json.getJSONObject("data").getString("allResult"));
                        tvtoday.setText(json.getJSONObject("data").getString("dayResult"));


                        for(int i=0;i<json.getJSONObject("data").getJSONArray("surveyList").length();i++){
                            MainBean bean = new MainBean();
                            bean.setId(json.getJSONObject("data").getJSONArray("surveyList").getJSONObject(i).getString("id"));
                            bean.setName(json.getJSONObject("data").getJSONArray("surveyList").getJSONObject(i).getString("name"));
//                            bean.setShortName(json.getJSONObject("data").getJSONArray("surveyList").getJSONObject(i).getString("shortName"));
                            bean.setCreateAt(json.getJSONObject("data").getJSONArray("surveyList").getJSONObject(i).getString("createAt"));
                            bean.setStatus(json.getJSONObject("data").getJSONArray("surveyList").getJSONObject(i).getString("status"));
                            bean.setStaffPhone(json.getJSONObject("data").getJSONArray("surveyList").getJSONObject(i).getString("staffPhone"));
                            bean.setIsAlter(json.getJSONObject("data").getJSONArray("surveyList").getJSONObject(i).getBoolean("isAlter"));
                            beans.add(bean);
                        }
                        lv.setAdapter(new LvAdapter(beans));

                    }else{
                        Toast.makeText(MainActivity.this,json.getString("message"),0).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.e("main",s);
                Toast.makeText(MainActivity.this,"请检查网络设置",0).show();
            }
        });
    }

    class LvAdapter extends BaseAdapter{


        private final List<MainBean> b;

        public LvAdapter(List<MainBean> beans) {
            this.b = beans;
        }

        @Override
        public int getCount() {
            return b.size();
        }

        @Override
        public Object getItem(int position) {
            return b.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder() ;
                convertView = View.inflate(MainActivity.this,R.layout.lv_pro,null);
                holder. tvName = (TextView) convertView.findViewById(R.id.tv_name);
                holder. tvDate = (TextView) convertView.findViewById(R.id.tv_date);
                holder. tvStatus = (TextView) convertView.findViewById(R.id.tv_status);
                holder. tvChange = (TextView) convertView.findViewById(R.id.tv_change);

            }else{
                holder = (ViewHolder) convertView.getTag();
            }



           holder. tvName.setText(b.get(position).getName());
           holder. tvDate.setText(b.get(position).getCreateAt());

            switch (b.get(position).getStatus()){
                case"1":
                    holder. tvStatus.setText("完毕");
                    break;
                case"2":
                    holder. tvStatus.setText("未注册");
                    break;

                case "-1":
                    holder. tvStatus.setText("不配合");
                    break ;
                case "-2":
                    holder. tvStatus.setText("不配合");
                    break;
                case "0":
                    holder. tvStatus.setText("数据错误");
                    break;
            }


            if(b.get(position).isIsAlter()){
                holder. tvChange.setVisibility(View.GONE);
            }else{
                holder.tvChange.setVisibility(View.VISIBLE);
            }


            holder.tvChange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this,Auth1Activity.class);
                    intent.putExtra("act","edit");
                    intent.putExtra("id",b.get(position).getId());
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }
            });


            return convertView;
        }

        class ViewHolder{
            TextView tvName,tvDate,tvStatus,tvChange;
        }

    }
}
